#include"campus_live_info_manager.h"
#include"../log/campus_logger.h"

CampusLiveInfoManager::CampusLiveInfoManager(CampusWholeServer* whole_server)
    : whole_server_(whole_server)
{
}
CampusLiveInfoManager::~CampusLiveInfoManager()
{
}

void CampusLiveInfoManager::handleLiveJoin(websocketpp::connection_hdl hdl, json JMsg)
{
    CampusLogger::info("CampusLiveInfoManager::handleLiveJoin");
    doAddLiveInfo(hdl, JMsg);
}

void CampusLiveInfoManager::handleLiveChat(websocketpp::connection_hdl hdl, json JMsg)
{
    CampusLogger::info("CampusLiveInfoManager::handleLiveChat");
    doSendLiveChat(hdl, JMsg);
}

void CampusLiveInfoManager::handleLiveLeave(websocketpp::connection_hdl hdl)
{
    CampusLogger::info("CampusLiveInfoManager::handleLiveLeave");
    doRemoveLiveInfo(hdl);
}

void CampusLiveInfoManager::doAddLiveInfo(websocketpp::connection_hdl hdl, json JMsg)
{
    CampusLogger::info("CampusLiveInfoManager::doAddLiveInfo");
    if(live_join_map_.find(hdl) == live_join_map_.end()){
        live_join_map_[hdl] = CampusLiveInfo();
        live_join_map_[hdl].uid = JMsg["uid"];
        live_join_map_[hdl].live_name = JMsg["live_name"];
        live_join_map_[hdl].chat_serial = -1;

        //发送加入直播成功的消息
        JMsg["cmd"] = SIGNAL_TYPE_RESP_LIVE_JOIN;
        JMsg["ret"] = CampusLiveRet::LIVE_JOIN_RET_SUCCESS;
        
        whole_server_->send(hdl, JMsg.dump());
    }
    else{
        CampusLogger::warn("CampusLiveInfoManager::doAddLiveJoin: hdl already in live_join_map_");
        
        json JMsg;
        JMsg["cmd"] = SIGNAL_TYPE_RESP_LIVE_JOIN;
        JMsg["ret"] = CampusLiveRet::LIVE_JOIN_RET_FAIL;
        
        whole_server_->send(hdl, JMsg.dump());
    }

}

void CampusLiveInfoManager::doSendLiveChat(websocketpp::connection_hdl hdl, json JMsg)
{
    CampusLogger::info("CampusLiveInfoManager::doSendLiveChat");

    try{
        std::string chat_info = JMsg["chat_info"];
        int chat_serial = JMsg["chat_serial"];
        std::string live_name = JMsg["live_name"];
        std::string uid = JMsg["uid"];
      
        CampusLogger::info("CampusLiveInfoManager::doSendLiveChat: chat_info = {}, chat_serial = {}, live_name = {}, uid = {}",
             chat_info, chat_serial, live_name, uid);


        json respJson;
        respJson["cmd"] = SIGNAL_TYPE_RESP_LIVE_CHAT;
        respJson["ret"] = CampusLiveRet::LIVE_CHAT_RET_SUCCESS;
        respJson["chat_info"] = chat_info;
        respJson["chat_serial"] = chat_serial;
        respJson["live_name"] = live_name;
        respJson["uid"] = uid;  
        
        //遍历live_join_map_，如果live_name匹配，就发送聊天消息
        for(auto&[hdl, live_info]: live_join_map_){
            if(live_info.live_name == live_name){
                whole_server_->send(hdl, respJson.dump());
            }
        }

    }
    catch(...){
        json respJMsg;
        respJMsg["cmd"] = SIGNAL_TYPE_RESP_LIVE_CHAT;
        respJMsg["ret"] = CampusLiveRet::LIVE_CHAT_RET_FAIL;
        whole_server_->send(hdl, respJMsg.dump());

        CampusLogger::warn("CampusLiveInfoManager::doSendLiveChat: JMsg format error");
    }

}

void CampusLiveInfoManager::doRemoveLiveInfo(websocketpp::connection_hdl hdl)
{
    CampusLogger::info("CampusLiveInfoManager::doRemoveLiveInfo");
    if(live_join_map_.find(hdl) != live_join_map_.end()){
        std::string live_name = live_join_map_[hdl].live_name;

        live_join_map_.erase(hdl);
    }
    else{
        CampusLogger::info("CampusLiveInfoManager::doRemoveLiveInfo: hdl not in live_join_map_");
    }
}
